package errorhandling;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An error handler using aspect.
 */

@Aspect
public class ErrorHandler {

    @Around(
    // pointcut = "@around(@annotation(org.springframework.transaction.annotation.Transactional))",
    value = "@annotation(org.springframework.transaction.annotation.Transactional)"
    // argNames = "joinPoint, proceedingJoinPoint"
    )
    public Object processError(final ProceedingJoinPoint join) throws Throwable {
        try {
            return join.proceed();
        } catch (Exception e) {
            Logger log = LoggerFactory.getLogger(ErrorHandler.class);
            log.error(e.getMessage(), e);
            throw new ServiceException(e, "Error ocurred on service: " + e.getMessage());
        }
    }
}
